package sk.gtug.hackatlon.drunkdroid.models;

import java.util.LinkedList;
import java.util.List;
import java.util.Random;

import sk.gtug.hackatlon.drunkdroid.R;
import sk.gtug.hackatlon.drunkdroid.MainActivity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.os.Handler;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

/**
 * @author Lubos Omelina
 */

public class BeerManager {
	
	class Beer extends Collisional {
		private int level = 5;
		private ImageView image;
		public Beer(int x, int y, int radius,Context context){
			super(x,y,radius);
			image = new ImageView(context);
		}
		public void decreaseLevel() {
			switch (--level)
		  {
		  	  case -1:
				 image.setImageResource(R.drawable.explosion);
				  break;
			  case 0:
				 image.setImageResource(R.drawable.beer_0);
				  break;
			  case 1:
				 image.setImageResource(R.drawable.beer_1);
				  break;
			  case 2:
				 image.setImageResource(R.drawable.beer_2);
				  break;
			  case 3:
				 image.setImageResource(R.drawable.beer_3);
				  break;
			  case 4:
				 image.setImageResource(R.drawable.beer_4);
				  break;
				  
		  }
		  };
		public int getLevel() {return level;};
		public ImageView getImageView() {return image;}; 
	
	}
	
	private List<Beer> mBeers;
	private List<Beer> mOldBeers;
	private Handler mReportHandler; 
	private Context mContext;
	private RelativeLayout mLayout;
	private boolean mEnd = false;
	private final int mBeerMargin = 40;
	private int mNumberOfBeers;
	private int mCatchedBeers;
	
	public BeerManager(){
		mBeers = new LinkedList<Beer>();
		mOldBeers = new LinkedList<Beer>();
		mReportHandler = new Handler();
 	}
	
	private void addBeer(int x, int y){
		Beer b = new Beer(x,y,15,mContext);
		mBeers.add(b);
		mLayout.addView(b.getImageView());
		RelativeLayout.LayoutParams absParams = 
            (RelativeLayout.LayoutParams)b.getImageView().getLayoutParams();
        absParams.leftMargin = x+13;
        absParams.topMargin = y+15;
        b.getImageView().setLayoutParams(absParams);
        b.decreaseLevel();
	}
	
	private void addRandomBeer(){
		int width = ((Activity)mContext).getWindowManager().getDefaultDisplay().getWidth()-2*mBeerMargin;
        int height = ((Activity)mContext).getWindowManager().getDefaultDisplay().getHeight()-2*mBeerMargin;
    	addBeer((int)(Math.random()*width+mBeerMargin),(int)(Math.random()*height+mBeerMargin));
	}
	
	private void addRandomBeers(int count){
		for(int i =0; i<count; i++){	
			addRandomBeer();
        }
	}
	
	public void start(int count,Activity context){
		mContext = context;
	    mLayout = (RelativeLayout)context.findViewById(R.id.beer_hunter_layout);
		for(Beer b: mBeers){
			mOldBeers.add(b);
		}
		removeOldBeers();
		mReportHandler.removeCallbacks(mUpdateTimeTask);
		mReportHandler.postDelayed(mUpdateTimeTask, 2000);
		mEnd=false;
		mCatchedBeers = 0;
		mNumberOfBeers = count;
		addRandomBeers(count);
	}
	
	public void stop(){
		mEnd = true;
		mReportHandler.removeCallbacks(mUpdateTimeTask);
	}
	
	public int catchedBeers(){
		return(mCatchedBeers);
	}
	
	private Runnable mUpdateTimeTask = new Runnable() {
	 	public void run() {
	 		 for(Beer b: mBeers)
	 		 {
	 			 if(b.getLevel()<1){
	 				mEnd = true;
	 			}
	 			b.decreaseLevel();
	 		 }
	 		 
	 		if(!mEnd){
	 			mReportHandler.postDelayed(this, 2000);
	 		} else{
	 			if ( MainActivity.mPlayer != null ) {
	 				MainActivity.mPlayer.play(MainActivity.mPlayGlass, 1, 1, 1, 0, 1);
	 			}
 				MessageBox("You're too druk!");
	 		}
	 	}
	 };
	
	public boolean hasEnded(){
		return mEnd;
	}
	public void checkCollision(Collisional obj){
		for(Beer b: mBeers){
			if(obj.isCollision(b)){
				mOldBeers.add(b);
				if ( MainActivity.mPlayer != null ) {
					MainActivity.mPlayer.play(MainActivity.mPlayBurp, 1, 1, 1, 0, 1);
				}
				mCatchedBeers++;
			}
		}
		removeOldBeers();
		addRandomBeers(mNumberOfBeers-mBeers.size());
	}
	
	private void removeOldBeers(){
		for(Beer b: mOldBeers){
			 mBeers.remove(b);
			 mLayout.removeView(b.getImageView());
		 }
		 mOldBeers.clear();
	}
	
	public void MessageBox(String message){
	    Toast.makeText(mContext,message,Toast.LENGTH_SHORT).show();
	}
}
